package com.example.imoocoperator.controller;

import com.example.imooccommon.entity.consts.ConstsCollege;
import com.example.imooccommon.page.TailPage;
import com.example.imooccommon.service.constsservice.ConstsCollegeServiceImpl;
import com.example.imooccommon.util.JsonView;
import com.qiniu.util.Json;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author Anita
 * @Date 2018/1/17 11:05
 */
@Controller
@Slf4j
@RequestMapping("/college")
public class CollegeController {

    @Autowired
    private ConstsCollegeServiceImpl collegeService;

    /**
     * 分页
     */
    @RequestMapping(value = "/queryPageList")
    public ModelAndView queryPage(ConstsCollege constsCollege, TailPage<ConstsCollege> page,
                                  Map<String ,Object> map){
        log.info("pageTotalCount+{}",String.valueOf(page.getPageTotalCount()));
        String name = constsCollege.getName();
        if(StringUtils.isNotEmpty(name)){
            name = name.trim();
        }else{
            name  = null;
        }

        Page<ConstsCollege> constsColleges = collegeService.queryPage(name,page.getStartIndex(),page.getPageSize());
        page.setItemsTotalCount(constsColleges.getContent().size());
        page.setItems(constsColleges.getContent());
        map.put("constsColleges",constsColleges);
        map.put("constsCollege",constsCollege);
        map.put("page",page);
        map.put("curNav","college");
        return new ModelAndView("/operator/cms/college/collegePageList",map);
    }

    @RequestMapping("/getById")
    @ResponseBody
    public Object getById(Long id){
        return JsonView.render(collegeService.getById(id));
    }

    @RequestMapping("/doMerge")
    @ResponseBody
    public JsonView doMerge(ConstsCollege constsCollege){
        //如果id为空，说明为新建的，如果id不为空则为修改
        if(constsCollege.getId()==null){
            ConstsCollege tmpEntity = collegeService.getByCode(constsCollege.getCode());
            if(tmpEntity != null) {
                return JsonView.render(1,"此编码已经存在");
            }
            collegeService.create(constsCollege);
            return  new JsonView(0,"success");
        }else{
            ConstsCollege tmpEntity = collegeService.getByCode(constsCollege.getCode());
            if(tmpEntity!=null&&!tmpEntity.getId().equals(constsCollege.getId())){
                return JsonView.render(1,"此编码已经存在");
            }
            tmpEntity.setName(constsCollege.getName());
            collegeService.create(tmpEntity);
            return  new JsonView(0,"success");
        }
    }


    @RequestMapping("/deleteLogic")
    @ResponseBody
    public JsonView deleteLogic(ConstsCollege constsCollege){
        collegeService.deleteLogic(constsCollege);
        return new JsonView(0,"success");
    }

}
